package com.angel.adapter;

import java.util.List;

import com.angel.activity.R;
import com.angel.entity.SearchActivity;
import com.angel.entity.SearchActivityList;
import com.angel.util.AsyncDownload;
import com.angel.util.BaseUtil;
import com.angel.util.HttpUtil;
import com.angel.util.AsyncDownload.CallBack;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/** 自定义配器Adapter */
public class MyAdapter extends BaseAdapter {
	private Context context = null;
	private List<SearchActivity> list;
	/**
	 * 自定义构造
	 * 
	 * @param activity
	 * @param handler
	 * @param list
	 */
	public MyAdapter(Context context,  List<SearchActivity> list) {
		this.context = context;
		this.list=list;
	}

	@Override
	public int getCount() {
		return this.list.size()==0?0:list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position)==null?null:list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	/*刷新Adapter*/
	public void refresh(List<SearchActivity> refreshList){
		list.addAll(refreshList);
		this.notifyDataSetChanged();
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		MyViewHolder holder = null;
		View v=convertView;
//		if (v == null) {
			holder = new MyViewHolder();
			v = LayoutInflater.from(context).inflate(
					R.layout.my_item, null);
			holder.titletextView = (TextView) v 
					.findViewById(R.id.my_title);
			String title=list.get(position).getActivityTitle();
//			if(title.length()>13){//判断标题的长度
//				holder.titletextView.setText(title.substring(0,11)+"...");
//			}else{
				holder.titletextView.setText(title);
//			}
			holder.timetextView = (TextView) v 
					.findViewById(R.id.my_time);
			holder.timetextView.setText(list.get(position).getActivityTime());
			holder.addresstextView = (TextView) v 
					.findViewById(R.id.my_address);
			holder.addresstextView.setText(list.get(position).getActivityAddress());
			holder.jiontextView = (TextView) v 
					.findViewById(R.id.my_join_id);
			holder.jiontextView.setText(list.get(position).getActivityPartipateNum());
			
			holder.intereststextView = (TextView) v 
					.findViewById(R.id.intetst_txt_id);
			holder.intereststextView.setText(list.get(position).getActivityInterestNum());
			holder.endtextView = (TextView) v .findViewById(R.id.finish_time_txt_id);
			String finishDate=list.get(position).getFinishDate();
			if(finishDate.contains("-")){//判断时间是否为负数
				holder.endtextView.setText("已结束");
				holder.endtextView.setTextColor(Color.RED);
			}else{
				holder.endtextView.setText(finishDate+"天后");
			}
			
			
			holder.picimageView = (ImageView) v 
					.findViewById(R.id.my_pic_id);
			//异步加载图片
			AsyncDownload download=AsyncDownload.getInstance();
//			String imgUrl=HttpUtil.picUrl+"angel/"+list.get(position).getSearchMainImgUrl();
			String imgUrl=BaseUtil.getPicUrl(context,list.get(position).getSearchMainImgUrl());
			Drawable drw=download.imageDown(holder.picimageView,imgUrl, new CallBack(){
				public void imgDown(ImageView view,Drawable drawable){
				//	view.setBackgroundDrawable(drawable);
					view.setImageDrawable(drawable);
				}
			});
			if(drw!=null){
				holder.picimageView .setImageDrawable(drw);
			}
			v.setTag(holder);
			Log.i("MyAdapter","MyAdapter-->posistion:"+position);
//		} else {
//			holder = (MyViewHolder) v.getTag();
//			int w=holder.picimageView.getWidth();
//			int h=holder.picimageView.getHeight();
//			Log.i("MyAdapter","MyAdapter-->getWidth"+w+" getHeight"+h);
//		}
		return v;
	}
}

class MyViewHolder {
	TextView titletextView;//活动标题
	TextView timetextView;//活动举行时间
	TextView addresstextView;//活动地址
	TextView jiontextView;//参加活动人数
	TextView intereststextView;//感兴趣人数
	TextView endtextView;//活动完成时间
	ImageView picimageView;//活动图片
}
